# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.reportCartDao import ReportCarDao
import time
from . import app
from com.zzh.dao_v2.AuthTokenOperation import authTokenOperation
localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))


CORS(app, supports_credentials=True)

"""
       表名：zzh_report_cart

说明：申报车表
"""

@app.route('/queryReportCarById', methods=['POST'])
def queryReportCarByIdNoToken():

    if not request.json:
        abort(400)
    id = request.json.get("id")
    try:
        total_count, userCodeJSON= ReportCarDao().queryReportCarById(id)
        if total_count:
            return jsonify({"message": {"code": 1, "status": "queryReportCar OK"},"info":userCodeJSON})
        else:
            return jsonify({"info": [], "message": {"code": 0, "status": "No data"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})

@app.route('/queryReportCarByCondition', methods=['POST'])
def queryReportCarByConditionNoToken():

        if not request.json:
            abort(400)
        try:
            page = request.json['page']
            pageIndex = page["pageIndex"]
            pageSize = page["pageSize"]
        except:
            return jsonify({"message": {"code": 0, "status": "请传入 pageIndex和pageSize"}})
        try:
            conditions = request.json['condition']
        except:
            return jsonify({"message": {"code": 0, "status": "未传入指定condition"}})
        try:
            multiSort = request.json['multiSort']
        except:
            return jsonify({"message": "未传入排序规则multiSort"})
        try:
            total_count, UserCodeJSON = ReportCarDao().queryReportCarByCondition(pageIndex=pageIndex, pageSize=pageSize,
                                                                                 conditions=conditions, multiSort=multiSort)
            if total_count:
                return jsonify(
                    {"message": {"code": 1, "status": "queryReportCar OK"}, "total": total_count, "info": UserCodeJSON})
            else:
                return jsonify({"info": [], "message": {"code": 0, "status": "No data"}})
        except Exception, e:
                return jsonify({"message": {"code": 0, "info": str(e)}})

@app.route('/insertReportCar', methods=['POST'])
def insertReportCarNoToken():

        if not request.json:
            abort(400)


        Info = {

            'userId': request.json.get('userId'),  #
            'itemId': request.json.get('itemId'),  #
            'carStatus': request.json.get('isLock')  #
        }
        if not Info:
            return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})
        try:
            rs = ReportCarDao().insertReportCar(Info)
            if rs:
                return jsonify(
                    {"massage": {"code": 1, "info": "addReportCar OK"}})  # 添加函数调用成功，返回添加的信息（不包括userRecharge的Id）
            else:
                return jsonify({"message": {"code": 0, "info": "addReportCar Error"}})  # 添加函数调用失败
        except Exception, e:
            return jsonify({"message": {"code": 0, "info": str(e)}})





@app.route('/updateReportCar', methods=['POST'])
def updateReportCarNoToken():
    try:
        id = request.json['id']
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 id"}})
    oldInfo = ReportCarDao().selectReportCar(id)

    Info = {
         'id': request.json.get('id'),  #
        'userId': request.json.get('userId',oldInfo[1]),  #
        'itemId': request.json.get('itemId',oldInfo[2]),  #
        'carStatus': request.json.get('carStatus',oldInfo[3])  #

    }
    if not Info:
        return jsonify({"message": {"code": 0, "info": "信息填写未完整"}})
    try:
        rs = ReportCarDao().updateReportCar(Info)
        if rs:
            return jsonify(
                {"info": Info, "massage": {"code": 1, "info": "updateReportCar OK"}})  # 添加函数调用成功，返回添加的信息
        else:
            return jsonify({"message": {"code": 0, "info": "updateReportCar Error"}})  # 添加函数调用失败
    except Exception, e:
        return jsonify({"message": {"code": 0, "info": str(e)}})




@app.route('/deleteReportCar', methods=['POST'])  # 根据id数组删除zzh_user_Code表指定记录,传入格式为 "ids":[1,2,3,4]
def deleteReportCarNoToken():
    ids = request.json.get("ids")
    if not ids:
        return jsonify({"message":"请输入待删除的  id数组！"})
    try:
        rs = ReportCarDao().deleteReportCar(ids)
        if rs:
            return jsonify({"message": {"code": 1, "info": "deleteReportCar OK"}})
        else:
            return jsonify({"message": {"code": 0, "info": "deleteReportCar Error"}})
    except Exception, e:
            return jsonify({"message": {"code": 0,  "info": str(e)}})



@app.route('/logicDeleteReportCar', methods=['POST'])
def logicDeleteReportCarNoToken():
        if not request.json:
            abort(400)


        try:
            userId = request.json.get('userId'),  #
            itemId = request.json.get('itemId'),  #
        except:
            return jsonify({"message": {"code": 0, "status": "请传入 userId和itemId"}})

        try:
            rs = ReportCarDao().logicDeleteReportCar(userId, itemId)
            if rs:
                return jsonify(
                    {"massage": {"code": 1, "info": "logicDeleteReportCar OK"}})  # 添加函数调用成功，返回添加的信息
            else:
                return jsonify({"message": {"code": 0, "info": "logicDeleteReportCar Error"}})  # 添加函数调用失败
        except Exception, e:
                return jsonify({"message": {"code": 0, "info": str(e)}})








